<template>
  <div>
    <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>辅导员信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
      <div>
        <!--    功能区-->
        <div style="margin: 10px 0">
          <!--    搜索区-->
          <div style="margin: 10px 0">
            <el-input
              v-model="search"
              clearable
              placeholder="请输入姓名"
              prefix-icon="Search"
              style="width: 20%"
            />
            <el-button
              icon="Search"
              style="margin-left: 5px"
              type="primary"
              @click="load"
            ></el-button>
            <el-button
              icon="refresh-left"
              style="margin-left: 10px"
              type="default"
              @click="reset"
            ></el-button>
            <div style="float: right">
              <el-tooltip content="添加" placement="top">
                <el-button
                  icon="plus"
                  style="width: 50px"
                  type="primary"
                  @click="add"
                ></el-button>
              </el-tooltip>
            </div>
          </div>
        </div>
        <!--    表格-->
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          max-height="705"
          style="width: 100%"
        >
          <el-table-column label="#" type="index" />
          <el-table-column label="学院" prop="teacher.academy" />
          <el-table-column label="班级" prop="teacher.classname" />
          <el-table-column label="账号" prop="teacher.username" sortable />
          <el-table-column label="姓名" prop="teacher.name" />
          <el-table-column
            :filter-method="filterTag"
            :filters="[
              { text: '男', value: '男' },
              { text: '女', value: '女' },
            ]"
            filter-placement="bottom-end"
            label="性别"
            prop="teacher.gender"
          />
          <el-table-column label="年龄" prop="teacher.age" sortable />
          <el-table-column label="手机号" prop="teacher.phone" />
          <el-table-column
            :show-overflow-tooltip="true"
            label="邮箱"
            prop="teacher.email"
                   width="200"
          />
          <el-table-column label="角色" prop="roleName" />
          <!--      操作栏-->
          <el-table-column label="操作" width="130px"  v-if="this.identity === 'admin'">
            <template #default="scope">
              <el-button
                icon="Edit"
                type="primary"
                @click="handleEdit(scope.row)"
                >编辑</el-button
              >

              <el-popconfirm
                title="确认删除？"
                @confirm="handleDelete(scope.row.teacher.userId)"
              >
                <template #reference>
                  <el-button icon="Delete" type="danger"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div style="margin: 10px 0">
          <el-pagination
            v-model:currentPage="currentPage"
            :page-size="pageSize"
            :page-sizes="[10, 20]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </div>
        <div>
          <!--      弹窗-->
          <el-dialog
            v-model="dialogVisible"
            title="操作"
            width="30%"
            @close="cancel"
          >
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="120px"
            >
              <el-form-item label="学号" prop="username">
                <el-input
                  v-model="form.username"
                  :disabled="judgeAddOrEdit"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input
                  v-model="form.password"
                  :disabled="disabled"
                  :show-password="showpassword"
                  style="width: 80%"
                ></el-input>
                <el-tooltip content="修改密码" placement="right">
                  <el-icon
                    :style="editDisplay"
                    size="large"
                    style="margin-left: 5px; cursor: pointer"
                    @click="EditPass"
                  >
                    <edit />
                  </el-icon>
                </el-tooltip>
              </el-form-item>
              <el-form-item :style="display" label="确认密码" prop="checkPass">
                <el-input
                  v-model="form.checkPass"
                  :show-password="showpassword"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" style="width: 80%"></el-input>
              </el-form-item>
              <el-form-item label="年龄" prop="age">
                <el-input
                  v-model.number="form.age"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
              <el-form-item label="性别" prop="gender">
                <el-radio v-model="form.gender" label="男">男</el-radio>
                <el-radio v-model="form.gender" label="女">女</el-radio>
              </el-form-item>
              <el-form-item label="手机号" prop="phone">
                <el-input
                  v-model.number="form.phone"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱地址" prop="email">
                <el-input v-model="form.email" style="width: 80%"></el-input>
              </el-form-item>
              <el-form-item label="角色" prop="roleId">
                <el-select
                  v-model="form.roleId"
                  placeholder="请选择角色"
                  style="width: 80%"
                >
                  <el-option
                    v-for="dorm in roleList"
                    :key="dorm.id"
                    :label="dorm.roleName"
                    :value="dorm.id"
                  />
                  <!-- <el-option label="一号楼" value="一号楼" />
            <el-option label="二号楼" value="二号楼" />
            <el-option label="三号楼" value="三号楼" />
            <el-option label="四号楼" value="四号楼" /> -->
                </el-select>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
              </span>
            </template>
          </el-dialog>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script >
import request from "@/utils/request";

const { ElMessage } = require("element-plus");

export default {
  name: "StuInfo",
  data() {
    // 手机号验证
    const checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|6|7|8][0-9]{9}$/;
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(value)) {
            callback();
          } else {
            callback(new Error("电话号码格式不正确"));
          }
        }
      }, 100);
    };
    const checkPass = (rule, value, callback) => {
      if (!this.editJudge) {
        if (value == "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.form.password) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      showpassword: true,
      judgeAddOrEdit: true,
      loading: true,
      editJudge: true,
      disabled: false,
      judge: false,
      dialogVisible: false,
      search: "",
      currentPage: 1,
      pageSize: 10,
      total: 0,
      identity:"",
      tableData: [],
      rname: [],
      roleList: [],
      form: {
        username: "",
        name: "",
        age: "",
        gender: "",
        phoneNum: "",
        email: "",
        roleName: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入学号", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{4,9}$/,
            message: "必须由 2 到 5 个字母或数字组成",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            pattern: /^(?:[\u4E00-\u9FA5·]{2,10})$/,
            message: "必须由 2 到 10 个汉字组成",
            trigger: "blur",
          },
        ],
        age: [
          { required: true, message: "请输入年龄", trigger: "blur" },
          { type: "number", message: "年龄必须为数字值", trigger: "blur" },
          {
            pattern: /^(1|[1-9]\d?|100)$/,
            message: "范围：1-100",
            trigger: "blur",
          },
        ],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        phoneNum: [{ required: true, validator: checkPhone, trigger: "blur" }],
        email: [
          { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 32,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        checkPass: [{ validator: checkPass, trigger: "blur" }],
      },
      editDisplay: {
        display: "block",
      },
      display: {
        display: "none",
      },
    };
  },
  created() {
    this.identity = JSON.parse(sessionStorage.getItem("identity"));
    
    this.find();
    this.getRoleList();
    this.loading = true;
    setTimeout(() => {
      //设置延迟执行
      this.loading = false;
    }, 1000);
  },
  methods: {
    async getRoleList(search) {
      request
        .get("/role/findrole", {
          params: {
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log("lsls", res);
          this.roleList = res.data; // 表格数据
          this.loading = false; // 关闭加载状态
        });
    },
    async find() {
      request
        .get("/teacher/findList", {
          params: {
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          console.log("total", this.total);
          this.loading = false;
        });
    },

    reset() {
      this.search = "";
      request
        .get("/stu/findList", {
          params: {
            pageNum: 1,
            pageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        });
    },
    filterTag(value, row) {
      return row.gender === value;
    },
    add() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        this.judgeAddOrEdit = false;
        this.editDisplay = { display: "none" };
        this.disabled = false;
        this.form = {};
        this.judge = false;
      });
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.judge === false) {
            //新增
            request.post("/teacher/add", this.form).then((res) => {
              console.log(res);
              if (res.code === "0") {
                ElMessage({
                  message: "新增成功",
                  type: "success",
                });
                this.search = "";
                this.find();
                this.dialogVisible = false;
              } else {
                ElMessage({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            //修改
            request.put("/teacher/update", this.form).then((res) => {
              console.log(res);
              if (res.code === "0") {
                ElMessage({
                  message: "修改成功",
                  type: "success",
                });
                this.search = "";
                this.find();
                this.dialogVisible = false;
              } else {
                ElMessage({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }
        }
      });
    },
    cancel() {
      this.$refs.form.resetFields();
      this.display = { display: "none" };
      this.editJudge = true;
      this.disabled = true;
      this.showpassword = true;
      this.dialogVisible = false;
    },
    EditPass() {
      if (this.editJudge) {
        this.showpassword = false;
        this.display = { display: "flex" };
        this.disabled = false;
        this.editJudge = false;
      } else {
        this.showpassword = true;
        this.display = { display: "none" };
        this.editJudge = true;
        this.disabled = true;
      }
    },
    //查询用户角色方法
    async getRoleByUserId(userid) {
      request.get("/role/getRoleByUserId/" + userid).then((res) => {
        console.log(res.data);
        this.form.roleId = res.data.roleId;
        console.log(this.form.roleId);
        this.loading = false;
      });
    },
    handleEdit(row) {
      //查询用户的角色
      console.log("row", row);
      console.log("row", row.student.userId);
      this.getRoleByUserId(row.student.userId);
      this.judge = true;
      // 生拷贝
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        this.form = JSON.parse(JSON.stringify(row.student));
        this.judgeAddOrEdit = true;
        this.editDisplay = { display: "block" };
        this.disabled = true;
      });
    },
    async handleDelete(username) {
      //删除
      console.log(username);
      request.delete("/teacher/delete/" + username).then((res) => {
        if (res.code === "0") {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          this.search = "";
        } else {
          ElMessage({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    handleSizeChange(pageSize) {
      //改变每页个数
      this.pageSize = pageSize;
      this.find();
    },
    handleCurrentChange(pageNum) {
      //改变页码
      this.currentPage = pageNum;
      this.find();
    },
  },
};
</script>